package com.ls.fw.web.file.servlet;

import java.io.File;

import javax.servlet.ServletException;

import org.apache.commons.lang3.StringUtils;

import com.ls.fw.web.core.servlet.BaseServlet;



/**
 *  savename:保存名称
 *  relativeDir:保存相对目录
 * @author Defender
 * 2014年6月19日下午9:59:48
 */
public abstract class BaseFileServlet extends BaseServlet{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	public static final String BASEDIR = "baseDir";
	public static final String ISABSOLUTEPATH = "isAbsolutePath";
	
	/**保存文件名称*/
	public static final String SAVENAME = "savename";
	
	/**保存文件相对目录*/
	public static final String RELATIVEDIR = "relativeDir";
	
	/**保存文件根目录*/
	protected String baseDir = "";
	/**设置的保存目录*/
	protected String relativeDirPara = "";
	protected boolean isAbsolutePath = false;
	/**项目路径*/
	protected String contextDir = "";
	
	@Override
	public void init() throws ServletException {
		super.init();
		this.contextDir = this.getServletContext().getRealPath("/");
		relativeDirPara =  this.getServletContext().getInitParameter(BASEDIR);
		String _isAbsolutePath =  this.getServletContext().getInitParameter(ISABSOLUTEPATH);
		this.handlPara(relativeDirPara, _isAbsolutePath);
	}
	
	
	protected void handlPara(String _baseDir,String _isAbsolutePath){
		String baseDir = _baseDir;
		if(!isEmpty(_isAbsolutePath)){
			try{
				if("true".equalsIgnoreCase(_isAbsolutePath)){
					this.isAbsolutePath = true ;
				} 
			}catch(Exception e){
				this.log(e.getMessage(), e);
			}
		}
		if(this.isEmpty(baseDir)){
			return ;
		}
		if(!this.isAbsolutePath && !this.isEmpty(baseDir)){
			// 设置文件上传路径
			String dir = baseDir;
			int index = 0;
			while(dir.startsWith("/")){
				dir = dir.replaceFirst("/", "");
			}
			while(dir.startsWith("\\")){
				dir = dir.replaceFirst("\\\\", "");
			}
			while(dir.startsWith("../")){
				dir = dir.replaceFirst("../", "");
				index++;
			}
			while(dir.startsWith("..\\")){
				dir = dir.replaceFirst("..\\\\", "");
				index++;
			}
			String upload = "";
			if(index>0){
				//this.isAbsolutePath = true ;
				upload = this.getServletContext().getRealPath("/");
				while(index>0){
					File f = new File(upload);
					upload = f.getParent();
					index--;
				}
				upload = upload+File.separator+dir;
			}else{
				upload = this.getServletContext().getRealPath(dir);
			}
			baseDir = upload;
		}
		if(this.isEmpty(baseDir)){
			baseDir = this.getServletContext().getRealPath("/");
		}
		this.log("载文件目录:"+baseDir);
		this.log("是否为绝对路径:"+isAbsolutePath);
		this.baseDir = checkDir(baseDir);
	}
	
	protected String getAbsolutePath(String path){
		String dir = path;
		int index = 0;
		while(dir.startsWith("/")){
			dir = dir.replaceFirst("/", "");
		}
		while(dir.startsWith("\\")){
			dir = dir.replaceFirst("\\\\", "");
		}
		while(dir.startsWith("../")){
			dir = dir.replaceFirst("../", "");
			index++;
		}
		while(dir.startsWith("..\\")){
			dir = dir.replaceFirst("..\\\\", "");
			index++;
		}
		dir = this.checkDir(dir);
		String upload = "";
		if(index>0){
			upload = this.getServletContext().getRealPath("/");
			while(index>0){
				File f = new File(upload);
				upload = f.getParent();
				index--;
			}
			upload = upload+File.separator+dir;
		}else{
			upload = this.getServletContext().getRealPath(dir);
		}
		return upload;
	}
	
	protected String checkDir(final String dirPath){
		String dir = dirPath;
	      try {
	    	    dir = this.checkSeparator(dir);
	    	    File file = new File(dir);
		  		if(!file.exists()){
		  			file.mkdirs();
		  		}
	      } catch (Exception e) {
	         this.log(e.getMessage()+",请设置下载文件目录！", e);
	      }
	      return dir;
	}
	
	protected String checkSeparator(final String dirPath){
		String dir = dirPath;
	      try {
	    	    dir = StringUtils.replaceChars(dir, "\\", File.separator);
	    	    dir = StringUtils.replaceChars(dir, "/", File.separator);
	      } catch (Exception e) {
	         this.log(e.getMessage()+",请设置下载文件目录！", e);
	      }
	      return dir;
	}
}
